﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace SampleWebSite.Controllers
{
    public partial class HomeController : Controller
    {
        public virtual ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

            return View();
        }

        public virtual ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public virtual ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Base()
        {
            //byte[] bytes = Encoding.Default.GetBytes("helloworld");
            //string str4 = Convert.ToBase64String(bytes);
            ////base 64 decode 
            //bytes = Convert.FromBase64String("OUU1OEI0Njc5QUY2QjIwMjc0NTZCRTE5NjdBMTk1NUM=");
            //var str5 = Encoding.Default.GetString(bytes);




            var str =
                "{\"tracesList\":[{\"logisticProviderID\":\"XLOBO\",\"mailNos\":\"DB060732763VW\",\"traces\":[{\"time\":\"2013-06-09 10:10:30\",\"desc\":\"01报关员 : 中kiei8&833***83734+)09098\",\"city\":\"上海\",\"facilityType\":\"sfacilitytype\",\"facilityNo\":\"sfacilityno\",\"facilityName\":\"sfacilityname\",\"action\":\"GOT\"}]}]}xlobo";
          
           // var str = "<order></order>123456";

            //var s1 = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            ////var s1 = SignMD5(str);
            //var s2 = Base64(s1);

            byte[] Original = Encoding.UTF8.GetBytes(str); //將字串來源轉為Byte[] 
            MD5 s1 = MD5.Create(); //使用MD5 
            byte[] Change = s1.ComputeHash(Original);//進行加密 
            var s2 = Convert.ToBase64String(Change);//將加密後的字串從byte[]轉回string


            //var s3= DeBase64("LghTkEmsD2tbQ3fsIBRcBg==");
            return null;
        }

        private string DeBase64(string lghtkemsd2tbq3fsibrcbg)
        {
            byte[] EncodedDataAsBytes = Convert.FromBase64String(lghtkemsd2tbq3fsibrcbg);
            var sDecode = Encoding.GetEncoding("GB2312").GetString(EncodedDataAsBytes);
            return sDecode;
        }

        public string Base64(string str)
        {
            byte[] bytes = Encoding.Default.GetBytes(str);
            try
            {
                var encode = Convert.ToBase64String(bytes);
                return encode;
            }
            catch
            {
            }
            return string.Empty;
        }

        public static string SignMD5(string parameterStr)
        {

            var query = new StringBuilder();
            query.Append(parameterStr);

            // 第三步：使用MD5加密
            var md5 = MD5.Create();
            byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(parameterStr));

            // 第四步：把二进制转化为大写的十六进制
            var result = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                string hex = bytes[i].ToString("X");
                if (hex.Length == 1)
                {
                    result.Append("0");
                }
                result.Append(hex);
            }

            return result.ToString();
        }
    }
}
